﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices.ComTypes;

namespace ClockwiseClient
{
    class ClassDrawGraph
    {
        public static void drawZoushitu(Graphics g, Rectangle rect, ClassStockItem stItem, ClassStockTick stTick, bool withFrame)
        {
            string text;
            const float labelWidth = 80;
            Point point = new Point(0, 0);
            Pen penRedDark = new Pen(Color.DarkRed, 1);
            Pen penRedDark2 = new Pen(Color.DarkRed, 2);
            Pen penYellow = new Pen(Color.Yellow, 1);
            Pen penWhite = new Pen(Color.White, 1);
            Pen penGray = new Pen(Color.Gray, 1);
            Font font = new Font("宋体", 12);
            StringFormat format = new StringFormat();
            g.FillRectangle(Brushes.Black, rect);
            if (withFrame)
            {
                Pen penRed = new Pen(Color.Red, 1);
                g.DrawLine(penRed, rect.Left, rect.Top, rect.Right, rect.Top);
                g.DrawLine(penRed, rect.Left, rect.Bottom, rect.Right, rect.Bottom);
                g.DrawLine(penRed, rect.Left, rect.Top, rect.Left, rect.Bottom);
                g.DrawLine(penRed, rect.Right, rect.Top, rect.Right, rect.Bottom);
            }
            float graphLeft = rect.Left + labelWidth;
            float graphRight = rect.Right - labelWidth;
            float graphTop = rect.Top + 10;
            float graphBottom = rect.Bottom - 10;
            g.DrawLine(penRedDark, graphLeft, graphTop, graphRight, graphTop);
            g.DrawLine(penRedDark, graphLeft, graphBottom, graphRight, graphBottom);
            g.DrawLine(penRedDark, graphLeft, graphTop, graphLeft, graphBottom);
            g.DrawLine(penRedDark, graphRight, graphTop, graphRight, graphBottom);
            format.LineAlignment = StringAlignment.Near;
            float high, temp;
            high = Math.Abs(stItem.f_high - stItem.f_preclose);
            temp = Math.Abs(stItem.f_preclose - stItem.f_low);
            if (high < temp) high = temp;
            float colLeft, colRight, colMiddle = 0;
            float rowHigh, rowLow, rowVolume, rowMiddle;
            rowVolume = rect.Top + 2 + rect.Height * 2 / 3;
            rowHigh = graphTop - 8;
            rowLow = rowVolume - 20;
            rowMiddle = (rowHigh + rowLow) / 2;
            colLeft = rect.Left + labelWidth - 2;
            colRight = graphRight + 2;
            float colTemp, rowTemp;
            float colBlock = (graphRight - graphLeft) / 8;
            float rowBlock = (rowLow - graphTop) / 8;
            int i;
            for (i = 1; i <= 7; i++)
            {
                // 画竖线
                colTemp = colBlock * i + graphLeft;
                if (i != 4)
                {
                    g.DrawLine(penRedDark, colTemp, graphTop, colTemp, rowLow);
                    g.DrawLine(penRedDark, colTemp, rowVolume, colTemp, graphBottom);
                }
                else
                {
                    g.DrawLine(penRedDark2, colTemp, graphTop, colTemp, graphBottom);
                    colMiddle = colTemp;
                }
                // 画横线
                rowTemp = rowBlock * i + graphTop;
                if (i != 4)
                {
                    g.DrawLine(penRedDark, graphLeft, rowTemp, graphRight, rowTemp);
                }
            }

            g.DrawLine(penRedDark, graphLeft, rowVolume, graphRight, rowVolume);
            g.DrawLine(penRedDark2, graphLeft, rowLow, graphRight, rowLow);
            g.DrawLine(penRedDark2, graphLeft, rowMiddle, graphRight, rowMiddle);

            float shuzhi;
            float fudu;

            // 价格上限
            shuzhi = stItem.f_preclose + high;
            text = shuzhi.ToString("f2");
            point.X = (int)colLeft;
            point.Y = (int)rowHigh;
            format.Alignment = StringAlignment.Far;
            g.DrawString(text, font, Brushes.Red, point, format);

            // 价格上限的涨幅
            fudu = stItem.f_preclose > 0 ? high * 100 / stItem.f_preclose : 0;
            text = fudu.ToString("f2") + "%";
            point.X = (int)colRight;
            point.Y = (int)rowHigh;
            format.Alignment = StringAlignment.Near;
            g.DrawString(text, font, Brushes.Red, point, format);

            // 价格下限
            shuzhi = stItem.f_preclose - high;
            text = shuzhi.ToString("f2");
            point.X = (int)colLeft;
            point.Y = (int)rowLow - 8;
            format.Alignment = StringAlignment.Far;
            g.DrawString(text, font, Brushes.Green, point, format);
            // 价格下限的跌幅
            fudu = stItem.f_preclose > 0 ? high * 100 / stItem.f_preclose : 0;
            text = fudu.ToString("f2") + "%";
            point.X = (int)colRight;
            point.Y = (int)rowLow - 8;
            format.Alignment = StringAlignment.Near;
            g.DrawString(text, font, Brushes.Green, point, format);

            // 价格中线，也就是昨日收盘价
            text = stItem.f_preclose.ToString("f2");
            point.X = (int)colLeft;
            point.Y = (int)rowMiddle - 8;
            format.Alignment = StringAlignment.Far;
            g.DrawString(text, font, Brushes.White, point, format);
            // 涨跌幅为0
            fudu = 0;
            text = fudu.ToString("f2") + "%";
            point.X = (int)colRight;
            point.Y = (int)rowMiddle - 8;
            format.Alignment = StringAlignment.Near;
            g.DrawString(text, font, Brushes.White, point, format);

            for (i = 1; i <= 7; i++)
            {
                if (i == 4) continue;
                rowTemp = rowBlock * i + graphTop;
                // 价格
                if (i < 4)
                    shuzhi = stItem.f_preclose + high * (4 - i) / 4;
                else
                    shuzhi = stItem.f_preclose - high * (i - 4) / 4;
                text = shuzhi.ToString("f2");
                point.X = (int)colLeft;
                point.Y = (int)rowTemp - 8;
                format.Alignment = StringAlignment.Far;
                g.DrawString(text, font, i < 4 ? Brushes.Red : Brushes.Green, point, format);

                // 涨跌幅
                fudu = stItem.f_preclose > 0 ? Math.Abs(shuzhi - stItem.f_preclose) * 100 / stItem.f_preclose : 0;
                text = fudu.ToString("f2") + "%";
                point.X = (int)colRight;
                point.Y = (int)rowTemp - 8;
                format.Alignment = StringAlignment.Near;
                g.DrawString(text, font, i < 4 ? Brushes.Red : Brushes.Green, point, format);

            }

            float maxVolume = 0;
            if (stTick != null)
            {
                for (i = 0; i < stTick.GetTickCount(); i++)
                {
                    ClassStockTick.StockTickItem item = stTick.GetTickItem(i);
                    if (maxVolume < item.delta) maxVolume = item.delta;
                }

                // 现量最大
                text = maxVolume.ToString("f0");
                point.X = (int)colLeft;
                point.Y = (int)rowVolume - 8;
                format.Alignment = StringAlignment.Far;
                g.DrawString(text, font, Brushes.Yellow, point, format);

                rowBlock = (graphBottom - rowVolume) / 4;
                for (i = 1; i <= 4; i++)
                {
                    shuzhi = maxVolume * (4 - i) / 4;
                    text = shuzhi.ToString("f0");
                    rowTemp = rowVolume + i * rowBlock;
                    point.X = (int)colLeft;
                    point.Y = (int)rowTemp - 8;
                    format.Alignment = StringAlignment.Far;
                    g.DrawString(text, font, Brushes.Yellow, point, format);
                    g.DrawLine(penRedDark, graphLeft, rowTemp, graphRight, rowTemp);
                }
            }

            point.Y = (int)rowLow + 2;
            format.Alignment = StringAlignment.Near;
            text = "09:30";
            point.X = (int)colLeft + 2;
            g.DrawString(text, font, Brushes.Gray, point, format);
            text = "13:00";
            point.X = (int)colMiddle + 2;
            g.DrawString(text, font, Brushes.Gray, point, format);

            format.Alignment = StringAlignment.Far;
            text = "11:30";
            point.X = (int)colMiddle - 2;
            g.DrawString(text, font, Brushes.Gray, point, format);
            text = "15:00";
            point.X = (int)colRight - 2;
            g.DrawString(text, font, Brushes.Gray, point, format);

            format.Alignment = StringAlignment.Center;
            text = "10:30";
            point.X = (int)(colMiddle + colLeft) / 2;
            g.DrawString(text, font, Brushes.Gray, point, format);
            text = "14:00";
            point.X = (int)(colMiddle + colRight) / 2;
            g.DrawString(text, font, Brushes.Gray, point, format);

            if (maxVolume > 0)
            {
                int step, nowMinute;
                float rowPjj = -1, rowZxj = -1, rowVol = -1, colLast = -1;
                float colStep = (colMiddle - colLeft) / 120;
                for (i = 0; i < stTick.GetTickCount(); i++)
                {
                    ClassStockTick.StockTickItem item = stTick.GetTickItem(i);
                    nowMinute = (int)item.time / 100;
                    if (nowMinute >= 930 && nowMinute <= 1130)
                    {
                        if (nowMinute <= 959)
                            step = nowMinute - 930;
                        else
                        if (nowMinute <= 1059)
                            step = nowMinute - 1000 + 30;
                        else
                            step = nowMinute - 1100 + 90;
                        colTemp = colLeft + colStep * step;
                    }
                    else
                    if (nowMinute >= 1300 && nowMinute <= 1500)
                    {
                        if (nowMinute < 1400)
                            step = nowMinute - 1300 + 120;
                        else
                        if (nowMinute < 1500)
                            step = nowMinute - 1400 + 180;
                        else
                            step = 240;

                        colTemp = colLeft + colStep * step;
                    }
                    else
                    if (nowMinute < 930)
                    {
                        colTemp = colLeft + 1;
                    }
                    else
                    if (nowMinute > 1500)
                    {
                        colTemp = colRight - 1;
                    }
                    else
                    if (nowMinute > 1130 && nowMinute < 1200)
                    {
                        colTemp = colMiddle - 1;
                    }
                    else
                    if (nowMinute > 1200 && nowMinute < 1300)
                    {
                        colTemp = colMiddle + 1;
                    }
                    else
                    {
                        continue;
                    }
                    if (colLast < 0) colLast = colTemp;

                    rowTemp = graphBottom - item.delta / maxVolume * (graphBottom - rowVolume);
                    g.DrawLine(penYellow, colTemp, rowTemp, colTemp, graphBottom);
                    rowTemp = graphBottom - item.volume / stItem.f_volume * (graphBottom - rowVolume);
                    if (rowVol < 0) rowVol = rowTemp;
                    g.DrawLine(penGray, colLast, rowVol, colTemp, rowTemp);
                    rowVol = rowTemp;

                    float pjj = item.volume > 0 ? item.amount / item.volume / 100 : item.preclose;
                    float zxj = item.price > 0 ? item.price : item.preclose;
                    float zsp = item.preclose;

                    if (pjj < item.low || pjj > item.high) pjj = zsp;

                    rowTemp = rowLow - (pjj - (zsp - high)) / (high * 2) * (rowLow - graphTop);
                    if (rowPjj < 0) rowPjj = rowTemp;
                    g.DrawLine(penYellow, colLast, rowPjj, colTemp, rowTemp);
                    rowPjj = rowTemp;

                    rowTemp = rowLow - (zxj - (zsp - high)) / (high * 2) * (rowLow - graphTop);
                    if (rowZxj < 0) rowZxj = rowTemp;
                    g.DrawLine(penWhite, colLast, rowZxj, colTemp, rowTemp);
                    rowZxj = rowTemp;

                    colLast = colTemp;
                }
            }

        }
        public static void drawKxiantu(Graphics g, Rectangle rect, ClassStockItem stItem, ClassStockDays stDays, bool withFrame)
        {
            g.FillRectangle(Brushes.Black, rect);
        }
    }
}
